package com.liukai.exam.service.impl;

import com.liukai.exam.mapper.*;
import com.liukai.exam.pojo.BigQuestion;
import com.liukai.exam.pojo.ExamManage;
import com.liukai.exam.pojo.exam.QueryForm;
import com.liukai.exam.service.IExamManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liukai.exam.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试卷管理 服务实现类
 * </p>
 *
 * @author liu kai
 * @since 2021-02-01
 */
@Service
public class ExamManageServiceImpl extends ServiceImpl<ExamManageMapper, ExamManage> implements IExamManageService {

    @Autowired
    private ExamManageMapper examManageMapper;

    @Autowired
    private ApplicationQuestionMapper applicationQuestionMapper;

    @Autowired
    private JudgeQuestionMapper judgeQuestionMapper;

    @Autowired
    private MoreChoiceQuestionMapper moreChoiceQuestionMapper;

    @Autowired
    private OneChoiceQuestionMapper oneChoiceQuestionMapper;

    @Autowired
    private FillQuestionMapper fillQuestionMapper;

    @Autowired
    private BigQuestionMapper bigQuestionMapper;

    /**
     * 新增试卷信息
     *
     * @param examManage
     * @return
     */
    @Override
    public int addExamManage(ExamManage examManage) {

        Long teacherId = examManage.getTeacherId();
        Integer classId = examManage.getClassId();

        // 根据教师编号和班级编号获取所有的试卷信息
        List<ExamManage> examManageList = examManageMapper.findExamNameById(teacherId, classId);
        for (ExamManage manage : examManageList) {
            // 如果新增的试卷的名称在该教师编号与班级编号下已存在，则不新增
            if (manage.getExamName().equals(examManage.getExamName())) {
                // 返回试卷信息新增失败，试卷名称已存在
                return Constant.EXAM_MANAGE_ADD_FILED_NAME_EXIST;
            }
        }

        return examManageMapper.addExamManage(examManage);
    }

    /**
     * 根据教师编号和班级编号获取所有的试卷信息
     *
     * @param teacherId
     * @param classId
     * @return
     */
    @Override
    public List<ExamManage> findExamNameById(Long teacherId, Integer classId) {
        return examManageMapper.findExamNameById(teacherId, classId);
    }

    /**
     * 根据教师编号和班级编号和试卷名称获取所有的试卷信息
     *
     * @param teacherId
     * @param classId
     * @return
     */
    @Override
    public ExamManage findExamNameByName(Long teacherId, Integer classId, String examName) {
        return examManageMapper.findExamNameByName(teacherId, classId, examName);
    }

    /**
     * 根据账号信息查询未完成创建的试卷信息
     *
     * @param teacherId
     * @param classId
     * @return
     */
    @Override
    public List<ExamManage> findNoDoneCreateExam(Long teacherId, Integer classId) {
//        System.out.println("findNoDoneCreateExam");
        List<ExamManage> noDoneCreateExamList = new ArrayList<>();
        // 1.获取该账号信息下的所有试卷信息
        List<ExamManage> examManageList = examManageMapper.findExamNameById(teacherId, classId);
        for (ExamManage examManage : examManageList) {
//            System.out.println(examManage.toString());
            // 2.获取每一套试卷的examId
            Long examId = examManage.getExamId();
            // 3.根据examId获取试卷大题类型
            List<BigQuestion> bigQuestionList = bigQuestionMapper.findBigQueById(examId);
            // 4.判断试卷大题类型是否为空，为空不获取未完成创建列表，跳出当前循环
            if (bigQuestionList.size() == 0)
                continue;
            // 5.判断试卷编号为examId的试卷是否完成创建
            boolean doneCreate = isDoneCreate(bigQuestionList, teacherId, classId);
            if (doneCreate == false)
                // 6.将未完成创建的试卷添加到列表中
                noDoneCreateExamList.add(examManage);
        }

        return noDoneCreateExamList;
    }

    /**
     * 根据账号信息查询已完成创建的试卷信息
     *
     * @param teacherId
     * @param classId
     * @return
     */
    @Override
    public List<ExamManage> findDoneCreateExam(Long teacherId, Integer classId) {

//        System.out.println("findDoneCreateExam");
        List<ExamManage> doneCreateExamList = new ArrayList<>();
        // 1.获取该账号信息下的所有试卷信息
        List<ExamManage> examManageList = examManageMapper.findExamNameById(teacherId, classId);
        for (ExamManage examManage : examManageList) {
//            System.out.println(examManage.toString());
            // 2.获取每一套试卷的examId
            Long examId = examManage.getExamId();
            // 3.根据examId获取试卷大题类型
            List<BigQuestion> bigQuestionList = bigQuestionMapper.findBigQueById(examId);
            // 4.判断试卷大题类型是否为空，为空不获取未完成创建列表，跳出当前循环
            if (bigQuestionList.size() == 0)
                continue;
            // 5.判断试卷编号为examId的试卷是否完成创建
            boolean doneCreate = isDoneCreate(bigQuestionList, teacherId, classId);
            if (doneCreate == true)
                // 6.将已经完成创建的试卷添加到列表中
                doneCreateExamList.add(examManage);
        }

        return doneCreateExamList;
    }

    /**
     * 条件查询获取试卷信息
     *
     * @param queryForm
     * @param list
     * @return
     */
    @Override
    public Map<String, Object> getExamManageByCondition(QueryForm queryForm, List<ExamManage> list) {

        Map<String, Object> map = new HashMap<>();
        List<ExamManage> examManageList = new ArrayList<>();
        ExamManage examManage = null;

        // 判断查询条件是否为空
        if (queryForm.getQueryCondition().equals("")) {
            // 查询条件为空，将试卷信息全部 添加进入新列表
            examManageList.addAll(list);
        } else {
            // 查询条件不为空，将满足条件的试卷信息添加进入新列表
            for (int i = 0; i < list.size(); i++) {
                examManage = list.get(i);
                // 判断该试卷信息是否满足条件
                if (examManage.getExamName().contains(queryForm.getQueryCondition())) {
                    // 满足条件，添加至新列表
                    examManageList.add(examManage);
                }
            }
        }
        // 获取满足条件的试卷信息
        map.put("count", examManageList.size());
        // 执行分页查询
        List<ExamManage> paginationSelectList = paginationSelect(queryForm, examManageList);
        map.put("list", paginationSelectList);
        return map;
    }

    /**
     * 根据账号信息和试卷名称删除试卷
     *
     * @param examManage
     * @return
     */
    @Override
    public int deleteExamManageById(ExamManage examManage) {

        bigQuestionMapper.deleteBigQuestionById(examManage.getExamId());

        return examManageMapper.deleteExamManageById(examManage.getTeacherId(),
                examManage.getClassId(), examManage.getExamId());
    }

    /**
     * 判断试卷是否完成创建
     *
     * @param bigQuestionList
     * @param teacherId
     * @param classId
     * @return
     */
    private boolean isDoneCreate(List<BigQuestion> bigQuestionList, Long teacherId, Integer classId) {

        for (BigQuestion bigQuestion : bigQuestionList) {
            Integer bigQuestionIndex = bigQuestion.getBigQuestionIndex();
            switch (bigQuestionIndex) {
                case Constant.ONE_CHOICE:
                    int oneChoiceCount = oneChoiceQuestionMapper.oneChoiceQuestionById(teacherId, classId);
                    if (oneChoiceCount == 0) {
                        return false;
                    }
                    break;
                case Constant.MORE_CHOICE:
                    int moreChoiceCount = moreChoiceQuestionMapper.moreChoiceQuestionById(teacherId, classId);
                    if (moreChoiceCount == 0) {
                        return false;
                    }
                    break;
                case Constant.JUDGE:
                    int judgeCount = judgeQuestionMapper.judgeQuestionById(teacherId, classId);
                    if (judgeCount == 0) {
                        return false;
                    }
                    break;
                case Constant.FILL:
                    int fillCount = fillQuestionMapper.fillQuestionById(teacherId, classId);
                    if (fillCount == 0) {
                        return false;
                    }
                    break;
                case Constant.APPLICATION:
                    int ApplicationCount = applicationQuestionMapper.applicationQuestionById(teacherId, classId);
                    if (ApplicationCount == 0) {
                        return false;
                    }
                    break;
                default:
                    break;
            }
        }
        return true;
    }

    /**
     * 分页查询
     *
     * @param queryForm
     * @param list
     * @return
     */
    private List<ExamManage> paginationSelect(QueryForm queryForm, List<ExamManage> list) {

        int count = list.size();
        int pageSize = queryForm.getPageSize();
        int pageNum = queryForm.getPageNum();
        int startNum = (pageNum - 1) * pageSize;
        int endNum = 0;
        if ((pageNum - 1) * pageSize + pageSize < count) {
            endNum = pageNum * pageSize;
        } else {
            endNum = count;
        }
        return list.subList(startNum, endNum);
    }

}
